<?php
/**
 * Created by PhpStorm.
 * User: shkj
 * Date: 2020/4/20
 * Time: 17:43
 */

namespace app\api\controller\v1;


use think\Db;
//use think\Request;
use think\facade\Request;
class Cart extends Base
{

    protected $beforeActionList = [
       'vali'
        //第一个''写前置方法的名字 ，后是需要用到前置方法的名字
    ];
       private $obj;

        protected function vali(){
        
        $data = Request::param();
        $token = Request()->header('Token');

        if(isset($data['uid'])&&!empty($data['uid'])&&$data['uid'] != 'undefined' && !empty($token)){
            $uid = $data['uid'];
            $status = $this->verifyToken($uid,$token);
        
              if($token && $status){
                $this->obj = model("cart");
        }
    }else{
            return_json(40001,'token过期了',40001);
                    }

        }




    public function addToCart(){
        $data=input('post.');
     
        $this->obj->addToCart($data);

     
    }

    public function updateAttr(){
        $data=input('get.');
        $this->obj->updateAttr($data);
    }

    //在购物车列表展示商品
    public function select(){
       $this->obj->getAll();
 
    }


    //编辑商品的数量
    public function updateNum(){
         $data=input('get.');
         $this->obj->edit($data);
             
    }




 

    //结算时查询的购物车详情
    public function detailCart(){
        $idAttr = Request::param('cartId');
        $cartGoodsRes=model('Cart')->getGoodsListInCart($idAttr);
        return_json(200,'查询成功',$cartGoodsRes);

    }


    //修改购物车商品数量
    public function updateCart(){
        $goodsNum=input('goods_number');
        $idAttr=input('rec_id');
        return model('Cart')->updateCart($idAttr,$goodsNum);
    }


    ///批量删除购物车记录
    public function deleteCartGoods() {
        $cartValue=input('cart_value'); //要批量删除的购物车记录 格式：16-74,76@15-68,69
        model('Cart')->deleteCartGoods($cartValue);
        return_json(200,'删除成功'); //删除成功
    }



}